package com.zhousb.deviceManager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhousb.deviceManager.mapper.*;
import com.zhousb.deviceManager.model.bo.AssetOutBo;
import com.zhousb.deviceManager.model.dto.AssetDto;
import com.zhousb.deviceManager.model.entity.*;
import com.zhousb.deviceManager.model.so.OutboundPageSo;
import com.zhousb.deviceManager.security.JwtUser;
import com.zhousb.deviceManager.service.AssetLifecycleService;
import com.zhousb.deviceManager.service.OutboundDeliveryService;
import com.zhousb.deviceManager.utils.AuthUtils;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class OutboundDeliveryServiceImpl implements OutboundDeliveryService {
    @Resource
    private KiOutboundDeliveryMapper kiOutboundDeliveryMapper;
    @Resource
    private KiAssetMapper kiAssetMapper;
    @Resource
    private AuthUtils authUtils;
    @Resource
    private AssetLifecycleService assetLifecycleService;
    @Autowired
    private KiFloorMapper kiFloorMapper;
    @Autowired
    private KiPositionMapper kiPositionMapper;
    @Autowired
    private KiAssetEpcRelationMapper kiAssetEpcRealtionMapper;

    @Override
    public boolean outStorage(AssetOutBo bo) {
        JwtUser user = authUtils.getUser();
        if (bo.getAssets() == null || bo.getAssets().isEmpty()) {
            return true;
        }


        LambdaQueryWrapper<KiAsset> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(KiAsset::getId, bo.getAssets());
        List<KiAsset> assets = kiAssetMapper.selectList(queryWrapper);


        List<Long> floorIds = assets.stream().map(KiAsset::getAssetFloor).toList();
        List<Long> positionIds = assets.stream().map(KiAsset::getAssetPosition).toList();

        Map<Long, String> floorMap = new HashMap<>();
        if (!floorIds.isEmpty()) {
            LambdaQueryWrapper<KiFloor> floorLambdaQueryWrapper = new LambdaQueryWrapper<>();
            floorLambdaQueryWrapper.in(KiFloor::getId, floorIds);
            List<KiFloor> floors = kiFloorMapper.selectList(floorLambdaQueryWrapper);
            floorMap = floors.stream().collect(Collectors.toMap(KiFloor::getId, KiFloor::getFloor));
        }


        Map<Long, String> positionMap = new HashMap<>();
        if (!positionIds.isEmpty()) {
            LambdaQueryWrapper<KiPosition> positionLambdaQueryWrapper = new LambdaQueryWrapper<>();
            positionLambdaQueryWrapper.in(KiPosition::getId, positionIds);
            List<KiPosition> positions = kiPositionMapper.selectList(positionLambdaQueryWrapper);
            positionMap = positions.stream().collect(Collectors.toMap(KiPosition::getId, KiPosition::getPosition));
        }
        List<AssetDto> assetDtos = new ArrayList<>();
        for (KiAsset s : assets) {
            AssetDto assetDto = new AssetDto();
            BeanUtils.copyProperties(s, assetDto);
            assetDto.setId(String.valueOf(s.getId()));
            assetDto.setClientId(String.valueOf(s.getClientId()));
            if (s.getAssetFloor() != null) {
                assetDto.setAssetFloor(floorMap.get(s.getAssetFloor()));
            }

            if (s.getAssetPosition() != null) {
                assetDto.setAssetPosition(positionMap.get(s.getAssetPosition()));
            }


            LambdaQueryWrapper<KiAssetEpcRelation> relationLambdaQueryWrapper = new LambdaQueryWrapper<>();
            relationLambdaQueryWrapper.eq(KiAssetEpcRelation::getAssetCode, s.getAssetCode());
            KiAssetEpcRelation kiAssetEpcRelation = kiAssetEpcRealtionMapper.selectOne(relationLambdaQueryWrapper);
            if (kiAssetEpcRelation != null) {
                assetDto.setAssetEpc(kiAssetEpcRelation.getEpc());
            }

            assetDtos.add(assetDto);
        }

        kiOutboundDeliveryMapper.batchInsert(user.getUserId(), bo.getReason(), assetDtos);

        List<KiAssetLifecycle> lifecycleList = new ArrayList<>();
        bo.getAssets().forEach(assetId -> {
            KiAssetLifecycle lifecycle = new KiAssetLifecycle();
            lifecycle.setAssetId(Long.valueOf(assetId));
            lifecycle.setEventType("出库");
            lifecycle.setEventContent("已出库");
            lifecycle.setEventOperator(user.getUserId());
            lifecycleList.add(lifecycle);

            kiAssetMapper.remove(assetId);
        });
        assetLifecycleService.batchInsert(lifecycleList, 100);
        return true;
    }

    @Override
    public IPage page(Page page, OutboundPageSo so) {
        return kiOutboundDeliveryMapper.page(page, so);
    }
}
